package com.meixiang.activity.account.manager;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;
import com.meixiang.util.DesUtil;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 修改登录密码
 *
 * @author zys
 */
public class ChangeLoginPasswordActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.cet_input_old_password)
    ClearEditText cetInputOldPassword;
    @Bind(R.id.cet_input_new_pwd)
    ClearEditText cetInputNewPwd;
    @Bind(R.id.cet_affirm_input_new_pwd)
    ClearEditText cetAffirmInputNewPwd;
    @Bind(R.id.tv_update_acknowledge)
    CheckEditTextEmptyButton tvUpdateAcknowledge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_login_pwd);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        title.setTitle(R.string.change_password);
        title.setLeftImageButton(R.mipmap.back);
        title.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void initView() {
        tvUpdateAcknowledge.setEditText(new EditText[]{cetInputOldPassword,cetInputNewPwd,cetAffirmInputNewPwd });
        cetInputOldPassword.setOnClickListener(this);
        cetInputNewPwd.setOnClickListener(this);
        cetAffirmInputNewPwd.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        tvUpdateAcknowledge.setOnClickListener(pwdChange);
    }

    @Override
    public void onClick(View v) {

    }

    private View.OnClickListener pwdChange = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (cetInputOldPassword.getText().length() < 6) {
                AbToastUtil.showToast(ChangeLoginPasswordActivity.this,"请输入6~16位的密码!");
                return;
            }
            if (cetInputNewPwd.getText().length() < 6) {
                AbToastUtil.showToast(ChangeLoginPasswordActivity.this,"请输入6~16位的密码!");
                return;
            }
            if (!cetAffirmInputNewPwd.getText().toString().equals(cetInputNewPwd.getText().toString())) {
                AbToastUtil.showToast(ChangeLoginPasswordActivity.this,"两次输入的密码不一致!");
                return;
            }
            getChangePassword();

        }
    };
    private  void getChangePassword(){
        HttpParams params = new HttpParams();
//        newPassword	String	必填			新的登录密码
//        oldPassword	String	必填
        params.put("oldPassword", DesUtil.md5(cetInputOldPassword.getText().toString().trim()));
        params.put("newPassword", DesUtil.md5(cetInputNewPwd.getText().toString().trim()));

        HttpUtils.post(Config.UPADTELOGIN_URL, params, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (code.equals("0")){
                    finish();
                    AbToastUtil.showToast(ChangeLoginPasswordActivity.this, message);
                }else {
                    cetInputOldPassword.setText("");
                    cetInputNewPwd.setText("");
                    AbToastUtil.showToast(ChangeLoginPasswordActivity.this, message);
                }
            }

            @Override
            public void onError(String code, String message) {
                cetInputOldPassword.setText("");
                cetInputNewPwd.setText("");
                AbToastUtil.showToast(ChangeLoginPasswordActivity.this, message);
            }

        });




    }

}
